原始笔记

Python CI

Python CI

正文

定制python CI

  1. On GitHub, navigate to the main page of the repository.

  2. Under your repository name, click  Actions.

    Screenshot of the tabs for the "github/docs" repository. The "Actions" tab is highlighted with an orange outline.

  3. If you already have a workflow in your repository, click New workflow.

  4. The "Choose a workflow" page shows a selection of recommended workflow templates. Search for "Python application".

  5. On the "Python application" workflow, click Configure.

  6. Edit the workflow as required. For example, change the Python version.

  7. Click Commit changes.

    The python-app.yml workflow file is added to the .github/workflows directory of your repository.

PyPy 是 Python 的另一种解释器实现(补充为什么要加上PyPy)

Using the setup-python action is the recommended way of using Python with GitHub Actions because it ensures consistent behavior across different runners and different versions of Python.''

Using multiple version Python template

name: Python package

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["pypy3.10", "3.9", "3.10", "3.11", "3.12", "3.13"]

    steps:
      - uses: actions/checkout@v6
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      # You can test your matrix by printing the current Python version
      - name: Display Python version
        run: python -c "import sys; print(sys.version)"

提供字段和yaml解释...

Using a specific version Python template

name: Python package

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6
      - name: Set up Python
        # This is the version of the action for setting up Python, not the Python version.
        uses: actions/setup-python@v5
        with:
          # Semantic version range syntax or exact version of a Python version
          python-version: '3.x'
          # Optional - x64 or x86 architecture, defaults to x64
          architecture: 'x64'
      # You can test your matrix by printing the current Python version
      - name: Display Python version
        run: python -c "import sys; print(sys.version)"

提供字段和yaml解释...

Exclude a specific version Python template

name: Python package

on: [push]

jobs:
  build:

    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        python-version: ["3.9", "3.11", "3.13", "pypy3.10"]
        exclude:
          - os: macos-latest
            python-version: "3.11"
          - os: windows-latest
            python-version: "3.11"

提供字段和yaml解释...

Installing dependencies

steps:
- uses: actions/checkout@v6
- name: Set up Python
  uses: actions/setup-python@v5
  with:
    python-version: '3.x'
- name: Install dependencies
  run: python -m pip install --upgrade pip setuptools wheel

提供字段和yaml解释...

Using requirements file

steps:
- uses: actions/checkout@v6
- name: Set up Python
  uses: actions/setup-python@v5
  with:
    python-version: '3.x'
- name: Install dependencies
  run: |
    python -m pip install --upgrade pip
    pip install -r requirements.txt

If you have a custom requirement or need finer controls for caching, you can use the cache action

Testing code

steps:
  - uses: actions/checkout@v6

  - name: Set up Python
    uses: actions/setup-python@v5
    with:
      python-version: '3.x'

  - name: Install dependencies
    run: |
      python -m pip install --upgrade pip
      pip install -r requirements.txt
      pip install pytest pytest-cov

  - name: Test with pytest
    run: |
      pytest tests/ --doctest-modules --junitxml=junit/test-results.xml --cov=你的包名 --cov-report=xml --cov-report=html

Switch to English